<?php
/**
 * 描述：资讯-列表。
 * @final
 */
final class Exec_News_List extends Exec_News_Public
{
    public function __construct()
    {
        parent::Get();

        #获取参数
        $type=Functions::UrlArgs('type');

        #定义标量
        $contentTitle=NULL;
        $params=array();

        #获取数据
        switch($type)
        {
            case 'class':                
                $classID=Functions::UrlArgs('id');
                if(!is_numeric($classID))
                {
                    echo 'error';
                    return;
                }
                $data=$this->Get_Class($classID);
                $params['id']=$classID;
                $contentTitle=DAO_NewsClass::GetNameByID($classID);
            break;
            case 'subclass':
                $subclassID=Functions::UrlArgs('id');
                if(!is_numeric($subclassID))
                {
                    echo 'error';
                    return;
                }
                $data=$this->Get_Subclass($subclassID);
                $params['id']=$subclassID;
                $contentTitle=DAO_NewsClass::GetNameByID($subclassID);
            break;
            case 'ranking':                
                $data=$this->Get_Ranking();
                $contentTitle='热点排行';
            break;
            case 'grade':
                $data=$this->Get_Ranking();
                $contentTitle='热点排行';
            break;
            case 'keywords':
                $keywords=Functions::UrlArgs('keywords');
                $params['keywords']=urlencode($keywords);
                $data=$this->Get_Keywords($keywords);
                $contentTitle=$keywords;
            break;
            case 'search':
                $contentTitle='资讯搜索';
                $keywords=trim(Functions::UrlArgs('keywords'));
                $params['keywords']=urlencode($keywords);
                $data=DB::PGetData("select * from news where M_IsDelete=FALSE and Title like ? order by ID desc limit ?,15",array(
                    '%'.$keywords.'%'
                ));
                foreach($data['Record'] as &$row)
                {
                    if($row['Thumbnail']){
                        $row['Thumbnail']=Config::PATH_RELATIVE_ATTACHMENT_NEWS.'/'.$row['Thumbnail'];
                    }
                }
            break;
            case 'latest':
                $contentTitle='最新资讯';
                $data=$this->Get_Latest();                
            break;
        }

        #配置参数
        $params['type']=$type;

        #渲染输出
        View::Assign('NewsList_Data',@$data['Record']);
        View::Assign('NewsList_PageMenu',PagerFactory::Create('Common')->Fetch($data,array
        (
            'URL'=>'/exec.php?action=news_list',
            'Parameters'=>$params,
            'IsDetail'=>TRUE,
            'Rewriter'=>($type=='search') ? NULL:new PagerRewrite('','type,id,keywords','html')
        )));
        View::Assign('ContentTitle',$contentTitle);
        View::Display('news/list.tpl');
    }

    /**
     * 描述：按大类获取数据。
     * @access private
     */
    private function Get_Class($classID)
    {
        #获取数据
        $data=DB::GetData('select * from news where ClassID='.$classID.' and M_IsDelete=FALSE order by ID desc limit ?,15');
        foreach($data['Record'] as &$row)
        {
            if($row['Thumbnail']){
                $row['Thumbnail']=Config::PATH_RELATIVE_ATTACHMENT_NEWS.'/'.$row['Thumbnail'];
            }
        }
        return $data;
    }

    /**
     * 描述：按小类获取数据。
     * @access private
     */
    private function Get_Subclass($subclassID)
    {
        #获取数据
        $data=DB::GetData('select * from news where SubclassID='.$subclassID.'  and M_IsDelete=FALSE order by ID desc limit ?,15');
        foreach($data['Record'] as &$row)
        {
            if($row['Thumbnail']){
                $row['Thumbnail']=Config::PATH_RELATIVE_ATTACHMENT_NEWS.'/'.$row['Thumbnail'];
            }
        }
        return $data;
    }

    /**
     * 描述：按热点排行获取数据。
     * @access private
     */
    private function Get_Ranking()
    {
        #获取数据
        $data=DB::GetData('select * from news where GroupID is NULL and M_IsDelete=FALSE and (ClassID='.DAO_NewsClass::SHIPYARD_ID.' or ClassID='.DAO_NewsClass::ANCILLARY_ID.' or ClassID='.DAO_NewsClass::SHIPPING_ID.' or ClassID='.DAO_NewsClass::MARITIMEWORKS_ID.') order by ID desc limit ?,15');
        foreach($data['Record'] as &$row)
        {
            if($row['Thumbnail']){
                $row['Thumbnail']=Config::PATH_RELATIVE_ATTACHMENT_NEWS.'/'.$row['Thumbnail'];
            }
        }
        return $data;
    }

    /**
     * 描述：按等级排序获取数据。
     * @access private
     */
    private function Get_Grade()
    {
        #获取数据
        $data=DB::GetData('select * from news where GroupID is NULL and M_IsDelete=FALSE order by M_IsHeadline desc,M_IsHeadline_Datetime desc,M_IsRecommend desc,M_IsRecommend_Datetime desc,M_IsTop desc,M_IsTop_Datetime desc,Datetime desc limit ?,15');
        foreach($data['Record'] as &$row)
        {
            if($row['Thumbnail']){
                $row['Thumbnail']=Config::PATH_RELATIVE_ATTACHMENT_NEWS.'/'.$row['Thumbnail'];
            }
        }
        return $data;
    }

    /**
     * 描述：按关键字获取数据。
     * @access private
     */
    private function Get_Keywords($keywords)
    {
        #获取数据
        $data=DB::PGetData("select * from news where ( concat('；',Keywords,'；') regexp concat('；(',replace(?,'；','|'),')；') ) and GroupID is NULL and M_IsDelete=FALSE order by ID desc limit ?,15",array(
            Filter::Mysql_Contact($keywords)
        ));
        foreach($data['Record'] as &$row)
        {
            if($row['Thumbnail']){
                $row['Thumbnail']=Config::PATH_RELATIVE_ATTACHMENT_NEWS.'/'.$row['Thumbnail'];
            }
        }
        return $data;
    }

    /**
     * 描述：按最新发布排序获取数据。
     * @access private
     */
    private function Get_Latest()
    {
        #获取数据
        $data=DB::GetData('select * from news where GroupID is NULL and M_IsDelete=FALSE order by ID desc limit ?,15');
        foreach($data['Record'] as &$row)
        {
            if($row['Thumbnail']){
                $row['Thumbnail']=Config::PATH_RELATIVE_ATTACHMENT_NEWS.'/'.$row['Thumbnail'];
            }
        }
        return $data;
    }
}
?>